package com.test.Servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownFileList extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String realPath = req.getRealPath("/fileUpload");
		File Directory = new File(realPath);
		List<HashMap> fileList = new ArrayList<HashMap>();
		if (Directory.isDirectory()) {
			File[] files = Directory.listFiles();
			for (File file : files) {
				if (!file.isDirectory()) {
					HashMap map = new HashMap();
					String fileName = file.getName();
					String filePath = file.getPath();
					map.put("name", fileName);
					map.put("path", filePath);
					fileList.add(map);
				}
			}
		}
		req.setAttribute("fileList", fileList);
		req.getRequestDispatcher("fileDown.jsp").forward(req, resp);
	}
}
